﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using WFT.Activities.Designers;
namespace WFT.Activities.Linq
{
    /// <summary>
    /// Return the last element of a sequence
    /// </summary>
    /// <typeparam name="TSource">Type of the Source</typeparam>
    [Description("Return the last element of a sequence")]
    [ToolboxBitmapAttribute(typeof(EntryPoint), "Resources.LinqToolbox.bmp")]
    [Designer(typeof(IconActivityDesigner))]
    [DesignerIcon("Resources/LinqDesigner.bmp")]
    public sealed class Last<TSource> : CodeActivity<TSource>
    {
        /// <summary>
        /// Source
        /// <remarks>
        /// Required Argument
        /// </remarks>
        /// </summary>
        [RequiredArgument]
        [Description("Source")]
        public InArgument<IEnumerable<TSource>> Source { get; set; }

        /// <summary>
        /// A function to test each element for a condition
        /// </summary>
        [Description("A function to test each element for a condition")]
        public InArgument<Func<TSource, bool>> Predicate { get; set; }

        protected override TSource Execute(CodeActivityContext context)
        {
            if (Predicate == null)
                return Source.Get(context).Last();
            else
                return Source.Get(context).Last(Predicate.Get(context));
        }
    }
}
